// 开始字符串，IE、火狐、谷歌不同
var ajaxfileuploadStartIE = '<pre>'.length;
var ajaxfileuploadStartFirefox = '<pre>'.length;
var ajaxfileuploadStartChrome = '<pre style="word-wrap: break-word; white-space: pre-wrap;">'.length;
// 结束字符串
var ajaxfileuploadEnd = '</pre>'.length;

/**
 * 根据浏览器不同，解析上传后的数据，转换为json
 * 
 * @param data
 *            上传后返回的数据
 * @returns 返回的JSON数据
 */
function getFileUploadReturnData(data) {
	// alert(data);
	var start = 0;
	var end = ajaxfileuploadEnd;
	if (isIE()) { // IE
		start = ajaxfileuploadStartIE;
	} else if (isFF()) {// 火狐
		start = ajaxfileuploadStartFirefox;
	} else { // 谷歌
		start = ajaxfileuploadStartChrome;
	}

	data = data.substring(start, data.length - end);
	// alert('deal:' + data);
	return JSON.parse(data);
}

/**
 * 附件上传
 * 
 * @param ctxPath
 *            项目路径
 * @param ids
 *            上传文件文本域的id值
 * @param jsonObject
 *            上传完成后添加附件表的其他参数信息
 * @param ulId
 *            附件上传后，新增li标签的ul的id值
 */
function fileupload(ctxPath, ids, jsonObject, ulId) {
	var array = ids.split(',');
	var files = [];
	for ( var i = 0; i < array.length; i++) {
		files[files.length] = array[i];
	}

	var url = "";
	// url = ctxPath + "/fileupload/servlet";
	url = ctxPath + '/fileupload';
	$.ajaxFileUpload({
		url : url,
		secureuri : false,
		fileElementId : files,
		dataType : 'text',
		beforeSend : function() {
		},
		complete : function() {
		},
		success : function(data) {
			data = getFileUploadReturnData(data);
			if (data.result == 'true') {
				fileuploadCallback(ctxPath, jsonObject, data.list, ulId);
			} else {
				alert(data.message);
			}
		}
	});

}

/**
 * 文件上传后执行保存数据库
 * 
 * @param ctxPath
 *            项目路径
 * @param jsonObject
 *            其他参数信息
 * @param list
 *            文件上传列表
 * @param ulId
 *            附件上传后，新增li标签的ul的id值
 */
function fileuploadCallback(ctxPath, jsonObject, list, ulId) {
	var url = ctxPath + "/project/projectAttachment/insert";
	for ( var i = 0; i < list.length; i++) {

		var id = list[i].guid;
		var filepath = list[i].pathname;
		var fileName = list[i].fileName;

		// 显示数据
		$('#' + ulId).append(getLiMsg(ctxPath, id, filepath, fileName));

		// 上传附件
		var param = new Object();
		param.projectCode = jsonObject.projectCode;
		param.id = id;
		param.fileName = fileName;
		param.path = filepath;
		param.fileSize = list[i].size;
		param.fileType = list[i].suffix;
		param.category = jsonObject.category;

		$.post(url, param, function(datas) {
		}, "json");
	}
}

/**
 * 获取附件li字符串
 * 
 * @param id
 *            附件id
 * @param filepath
 *            附件路径
 * @param fileName
 *            附件名称
 */
function getLiMsg(ctxPath, id, filepath, fileName) {
	var li = '';
	li += '<li>';

	li += '<span>';
	// 删除
	li += '<a ';
	li += ' href="javascript:void(0);" class="a-delete"';
	li += ' onclick="filedelete(\'' + ctxPath + '\',\'' + id + '\',\''
			+ filepath + '\',$(this));">';
	li += '删除';
	li += '</a>';
	li += '</span>';

	// 下载
	li += '<a target="_block"';
	li += ' href="' + ctxPath + "/filedownload?fileName=" + encodeURIComponent(fileName)
			+ "&filepath=" + encodeURIComponent(filepath) + '" title="'+fileName+'">';
	li += fileName;
	li += '</a>';

	li += '</li>';

	return li;
}

/**
 * 获取附件li字符串(只读)
 * 
 * @param id
 *            附件id
 * @param filepath
 *            附件路径
 * @param fileName
 *            附件名称
 */
function getLiMsgReadOnly(ctxPath, id, filepath, fileName) {
	var li = '';
	li += '<li>';

	// 下载
	li += '<a target="_block"';
	li += ' href="' + ctxPath + "/filedownload?fileName=" + encodeURIComponent(fileName)
		+ "&filepath=" + encodeURIComponent(filepath) + '" title="'+fileName+'">';
	li += fileName;
	li += '</a>';

	li += '</li>';

	return li;
}

/**
 * 删除
 * 
 * @param ctxPath
 *            ctxPath
 * @param id
 *            附件主键
 * @param aObj
 *            删除a标签
 */
function filedelete(ctxPath, id, filepath, aObj) {

	if (id == undefined || id == '' || $.trim(id).length == 0) {
		alert('附件不存在！');
	}
	var url = ctxPath + "/project/projectAttachment/deleteMsg?id=" + id;
	$.post(url, function(data) {
		data = JSON.parse(data);
		if (data.result == 'true') {
			aObj.parent().parent().remove();
			filedeleteCallback(ctxPath, filepath);
		} else {
			alert(data.message);
		}
	}, "text");
}

/**
 * 附件删除
 * 
 * @param ctxPath
 *            ctxPath
 * @param filepath
 *            附件路径
 */
function filedeleteCallback(ctxPath, filepath) {

	if (filepath == undefined || filepath == '' || $.trim(filepath).length == 0) {
		alert('附件不存在！');
	}

	var url = ctxPath + "/filedelete?filepath=" + filepath;
	$.post(url, function(data) {
	}, "json");
}

// 是否是IE
function isIE() {
	return $.browser.msie;
}
// 是否是火狐
function isFF() {
	return $.browser.mozilla;
}
// 是否是谷歌
function isChrome() {
	return $.browser.webKit;
}
